// view/login/login.js
let app = getApp()
let yuming = app.globalData.yuming
let reg = app.globalData.mobile
var ying_reg = app.globalData.ying_reg
var numzimu = app.globalData.numzimu
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    xuan: false, //是否打钩
    yuming: yuming,
    heights: "",
    title: '',
    buttonClicked: false,
    state: true, //0密码  1验证码
    mobile: '', //手机号
    get_code_btn: true,
    count_down: '60', //秒数
    code: '', //验证码
    shopCount: 0,
    superNum: 0,
    isxuan: true,
    isRotate: false, //是否加载旋转
    isFocus: true, // 是否聚焦
    rotate: false,
    is_phone: true,
    ischu: false,
    ischus: false,
    pass_val: '',
    ying_num: '',
  },

  // 忘记密码
  wangji() {
    wx.navigateTo({
      url: '/page_zhu/wangjimima/wangjimima?title=忘记密码&state=0',
    })
  },
  inputShowed() {
    this.setData({
      ischu: true
    })
  },
  bindblurs() {
    this.setData({
      ischu: false
    })
  },
  inputShoweds() {
    this.setData({
      ischus: true
    })

  },
  bindblurss() {
    this.setData({
      ischus: false
    })
  },
  click() {
    var that = this
    console.log(that.data.isRotate)
    that.setData({
      isRotate: true
    })
    setTimeout(function () {
      that.setData({
        isRotate: false
      })
      console.log(that.data.isRotate)
    }, 3000)
    var start_id = that.selectComponent("#start_id");
    start_id._rotate();


  },
  // 勾选协议
  xuanz() {
    if (this.data.xuan == true) {
      this.setData({
        xuan: false
      })
    } else {
      this.setData({
        xuan: true
      })
    }
  },
  // 倒计时
  get_code_settime: function (that) {
    var that = this;
    var countDown = that.data.count_down; //获取倒计时初始值
    //如果定时器设置在外边，那么用户就看不到countDown的数值变化，所以要把定时器存进data里边
    that.setData({
      //这里把setInterval复制给变量名timer
      timer: setInterval(function () {
        countDown--;
        that.setData({
          get_code_btn: false, //此处用于隐藏验证码点击按钮，使倒计时显示
          count_down: countDown //倒计时计数 
        })
        //当计数为0时，清空定时器，使验证码按钮再次显示
        if (countDown == 0) {
          clearInterval(that.data.timer); //此处我清除timer之后，count_down没有变回我原先设定的120，get_code_btn也没有变回默认的true，咱没搞明白，所以我又重新赋值了
          that.setData({
            get_code_btn: true,
            count_down: '60'
          })
        }
      }, 1000)
    })
  },
  // mobile
  mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 营业执照
  bindying(e) {
    this.setData({
      ying_num: e.detail.value
    })
  },
  // 密码
  bindpass(e) {
    this.setData({
      pass_val: e.detail.value
    })
  },
  tishi() {
    wx.showToast({
      title: '还有' + this.data.count_down + "秒才能再次获取",
      icon: 'none'
    })
  },
  // 点击获取验证码
  huoqu() {
    var that = this
    if (that.data.mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    } else if (that.data.mobile != '') {
      if (reg.test(that.data.mobile) == false) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none"
        })
      } else {
        that.yzm()
      }
    }
  },
  // 获取验证码接口
  yzm() {
    var that = this
    wx.request({
      url: 'https://msion.86wky.com/Besall/mobileCode',
      data: {
        mobile: that.data.mobile,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Token: wx.getStorageSync('token'),
      },
      method: 'post',
      success: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.setData({
          get_code_btn: false
        })
        that.get_code_settime()
      },
      fail: (error) => {
        reject(error);
      }
    })
  },
  // 二级账号
  erji() {
    wx.navigateTo({
      url: '/page_zhu/erjilogin/erjilogin',
    })
  },
  // 营业执照登录
  ying_login() {
    this.setData({
      is_phone: !this.data.is_phone,
      ying_num: '',
      pass_val: ''
    })
    // wx.navigateTo({
    //   url: '/page_zhu/ying_login/ying_login',
    // })
  },
  // 营业执照注册
  ying_zhu() {
    wx.showToast({
      title: '暂未开通',
      icon: 'none'
    })
    // wx.navigateTo({
    //   url: '/page_zhu/ying_zhu/ying_zhu',
    // })
  },
  // 协议
  xieyi() {
    wx.navigateTo({
      url: '/page_zhu/xieyi/xieyi?title=注册服务协议&state=regxieyi&st=0',
    })
  },
  // 条款
  tiaokuan() {
    wx.navigateTo({
      url: '/page_zhu/xieyi/xieyi?title=隐私权条款&state=ysqtk&st=0',
    })
  },
  //切换登录方式
  huan() {
    this.setData({
      state: !this.data.state
    })
  },
  // 验证码
  code(e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 登录验证
  queren() {
    if (this.data.is_phone) {
      if (this.data.mobile == '') {
        wx.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
      } else if (this.data.mobile != '') {
        if (reg.test(this.data.mobile) == false) {
          wx.showToast({
            title: '请输入正确的手机号',
            icon: "none"
          })
        } else {
          // if (this.data.code == '') {
          //   wx.showToast({
          //     title: '请输入验证码',
          //     icon: 'none'
          //   })
          // } else {
          //   this.logins()
          // }
            this.logins()

        }
      } else if (this.data.code == '') {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none'
        })
      } else if (this.data.code.length != 4) {
        wx.showToast({
          title: '请输入正确的验证码',
          icon: 'none'
        })
      } else {
        this.logins()
      }
    } else {
      if (this.data.ying_num == '') {
        wx.showToast({
          title: '请输入营业执照',
          icon: 'none'
        })
      } 
      // else if (ying_reg.test(this.data.ying_num) == false) {
      //   wx.showToast({
      //     title: '营业执照格式错误',
      //     icon: 'none'
      //   })
      // } 
      else if (this.data.pass_val == '') {
        wx.showToast({
          title: '请输入密码',
          icon: 'none'
        })
      } else if (numzimu.test(this.data.pass_val) == false) {
        wx.showToast({
          title: '密码格式错误',
          icon: 'none'
        })
      } else {
        this.ying_long()
      }
    }

  },
  // 营业执照登录
  ying_long() {
    if (this.data.xuan == false) {
      wx.showToast({
        title: '请勾选同意后再进行登录',
        icon: 'none'
      })
      this.setData({
        isxuan: false
      })
      var that = this
      setTimeout(function () {
        that.setData({
          isxuan: true
        })
      }, 1000)
    } else {
      this.setData({
        rotate: true
      })
      var that = this
      that.setData({
        buttonClicked: true
      })
      wx.request({
        url: yuming + 'shop/shop_creditcode_login',
        data: {
          creditcode: that.data.ying_num,
          password: that.data.pass_val,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Token: wx.getStorageSync('token'),
        },
        method: 'post',
        success: (res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          var data = res.data.data
          if (res.data.code == 200) {
            wx.setStorageSync('shopType', data.shopType)
            wx.setStorageSync('shopId', data.id)
            setTimeout(function () {
              var url = ""
              if (res.data.shopCount == 1) {
                url = "pages/index/index"
              } else {
                url = "page_zhu/xzdp/xzdp"
              }
              // shopType 0个体 1商户
              if (res.data.shopType == 0) {
                if (res.data.shopCount == 1) {
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
                } else {
                  wx.navigateTo({
                    url: '/page_zhu/xzdp/xzdp?mobile=' + that.data.mobile,
                  })
                }
              } else {
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }
            }, 500)
          } else {
            that.setData({
              buttonClicked: false,
              rotate: false
            })
          }
        },
        fail: (error) => {
          reject(error);
          that.setData({
            buttonClicked: false,
            rotate: false
          })
        }
      })
    }
  },
  // 登录接口
  logins() {
    if (this.data.xuan == false) {
      wx.showToast({
        title: '请勾选同意后再进行登录',
        icon: 'none'
      })
      this.setData({
        isxuan: false
      })
      var that = this
      setTimeout(function () {
        that.setData({
          isxuan: true
        })
      }, 1000)
    } else {
      this.setData({
        rotate: true
      })
      var that = this
      that.setData({
        buttonClicked: true
      })
      wx.request({
        url: 'https://msion.86wky.com/shop/shopDoLogin',
        data: {
          mobile: that.data.mobile,
          code: that.data.code,
          category: 1, //1验证码 2 否
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Token: wx.getStorageSync('token'),
        },
        method: 'post',
        success: (res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          setTimeout(function () {
            that.setData({
              rotate: false
            })
          }, 1000)
          if (res.data.code == 200) {
            // 0已注册 1 未注册
            var shopRes = res.data.shopRes
            wx.setStorageSync('shopRes', res.data.shopRes)
            setTimeout(function () {
              if (shopRes == 1) {
                if (wx.getStorageSync('token') == "") {
                  wx.navigateTo({
                    url: '/page_zhu/shou/shou?sta=0&url=page_zhu/zhuce/zhuce&mobile=' + that.data.mobile,
                  })
                } else {
                  wx.navigateTo({
                    url: '/page_zhu/zhuce/zhuce?mobile=' + that.data.mobile,
                  })
                }
              } else {
                wx.setStorageSync('shopType', res.data.shopType)
                wx.setStorageSync('shopId', res.data.shopId)
                // if (wx.getStorageSync('token') == "") {
                var url = ""
                if (res.data.shopCount == 1) {
                  url = "pages/index/index"
                } else {
                  url = "page_zhu/xzdp/xzdp"
                }
                // wx.navigateTo({
                //   url: '/page_zhu/shou/shou?sta=0&url='+url+'&mobile=' + that.data.mobile,
                // })
                // } else {
                // shopType 0个体 1商户
                if (res.data.shopType == 0) {
                  if (res.data.shopCount == 1) {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  } else {
                    wx.navigateTo({
                      url: '/page_zhu/xzdp/xzdp?mobile=' + that.data.mobile,
                    })
                  }
                } else {
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
                }
                // }

              }
            }, 1000)
          } else {
            that.setData({
              buttonClicked: false
            })
          }
        },
        fail: (error) => {
          reject(error);
        }
      })
      setTimeout(() => {
        this.setData({
          buttonClicked: false
        })
      }, 2000)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      code:''
    })
    this.other()
  },
  // 其他数据
  other() {
    var that = this
    wx.request({
      url: 'https://msion.86wky.com/shop/shoploginHtml',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Token: wx.getStorageSync('token'),
      },
      method: 'post',
      success: (res) => {
        var that = this
        that.setData({
          shopCount: res.data.shopCount,
          superNum: res.data.superNum
        })
      },
      fail: (error) => {
        reject(error);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      buttonClicked: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})